#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include "sys/ioctl.h"
#include "sys/select.h"
#include "poll.h"

#define KEY0VALUE 0XF0 /* 按键值 		*/
#define INVAKEY 0X00   /* 无效的按键值  */

#define CLOSE_CMD _IO(0xEF, 1)     // 关闭命令
#define OPEN_CMD _IO(0xEF, 2)      // 打开命令
#define SETPERIOD_CMD _IO(0xEF, 3) // 设置周期


int main(int argc, char *argv[]){
    struct pollfd fds;
    int fd,ret;
    char *filename;
    unsigned char data;

    if (argc != 2)
    {
        printf("ERROR usage!\r\n");
        return -1;
    }
    filename = argv[1];
    fd = open(filename, O_RDWR | O_NONBLOCK); // 以非阻塞方式打开文件
    if (fd < 0)
    {
        printf("file %s is open fail\r\n", filename);
        return -1;
    }
    else
    {
        printf("file %s is successful!!!\r\n", filename);
    }
    while (1) {
        fds.fd = fd;
        fds.events = POLLIN;
        ret = poll(&fds, 1, 500);
        if (ret < 0) {
            printf("poll is timeout\r\n");
        }else
        {
            if (fds.revents & POLLIN) {
                ret = read(fd, &data, sizeof(data));
                if (ret < 0) {

                }else{
                    if (data) {
                        printf("data is ===== %d\r\n", data);
                    }
                }
            }
        }
    }
}
